package org.software.views.student;

import org.software.entity.Student;
import org.software.service.StudentService;
import org.software.views.MenuFrame;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class StudentListFrame extends JFrame{

    private JTable studentTable;

    public StudentListFrame(){
        setTitle("学生列表");
        setSize(600,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        String[] columnNames ={"ID","学号","姓名","小组名"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames,0);

        List<Student> students = new StudentService().studentQuery();
        for(Student student : students){
            Object[] row ={student.getId(),student.getStudentId(),student.getStudentName(),student.getGroupName()};
            tableModel.addRow(row);
        }
        studentTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(studentTable);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        JButton backButton = new JButton("返回");

        buttonPanel.add(backButton);

        backButton.addActionListener(e ->
        {   dispose();
            new MenuFrame();
        });

        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        setVisible(true);
    }
}
